<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <new-page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    >
      <template v-slot:filter-button>
        <div class="flex_wrap_between_middle">
          <div style="display: flex; width: 50%">
            <p style="margin-right: 20px; max-width: 50%" class="text-style">
              产品名称: {{ info.commonName }}
            </p>
            <p class="text-style">生产批号: {{ info.batchNumber }}</p>
          </div>
          <div style="display: flex; width: 50%; justify-content: flex-end">
            <el-input
              v-model="filterInfo.keyword"
              :clearable="true"
              placeholder="请输入个体码"
              style="margin-right: 10px; width: 217px"
              @keyup.enter.native="search"
              @input="(e) => (filterInfo.keyword = validSe(e))"
            />
            <el-button @click="search">搜索</el-button>
          </div>
        </div>
      </template>
    </new-page-filter>
    <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
      <dialog-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        ref="depotTable"
        :api="tableInfo.api"
        v-model:data="tableInfo.data"
        :check-box="false"
        :tab-index="true"
        :listen-height="false"
        :pager="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
      >
        <template v-slot:col-consumeOrderNo="scope">
          <span
            class="slip-edit"
            @click="
              _toSourceDetail(scope.row.consumeOrderNo, scope.row.sourceId)
            "
            >{{ scope.row.consumeOrderNo || '-' }}</span
          >
          <!-- <span>{{ scope.row.consumeOrderNo || '-' }}</span> -->
        </template>
      </dialog-table>
    </el-form>
  </page-dialog>
</template>

<script setup name="IndividualDetails">
import { getConsignConsumeConfirmDetailsCustomizeListAll } from '@/api/stock.js'
import { getCustomizeSetting } from '@/api/customize'
import utils from '@/common/js/utils'
import { toSourceDetail } from '@/common/js/base.js'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  api: {
    type: Function,
    default: getConsignConsumeConfirmDetailsCustomizeListAll,
  },
  showDialog: {
    type: Boolean,
  },
  info: {
    type: Object,
    default: function () {
      return {}
    },
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  withSurgery: {
    type: Boolean,
    default: function () {
      return false
    },
  },
  consumeSourceId: {
    type: Number,
    default: -2,
  },
})

// 过滤相关配置
const filterInfo = ref({
  keyword: '',
  query: {
    queryMap: {
      ...props.query,
    },
    queryParams: {},
    customizeUserVO: {},
  },
  list: [{ slot: true, value: 'button' }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  copyData: [],
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  title: '个体详情',
  width: '90%',
  btLoading: false,
  btList: [{ label: '关闭', event: handleClose, show: true }],
})
const detailId = ref(undefined)

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件showDialog
  },
})

setData()
function setData() {
  let obj = {
    orderNo: {
      label: '单号',
      value: 'orderNo',
      componentType: 'input',
      show: false,
      type: 'column',
      hidden: true,
    },
    consumeOrderNo: {
      label: '单号',
      value: 'consumeOrderNo',
      componentType: 'input',
      type: 'slot',
      show: false,
      hidden: true,
    },
    price: {
      label: '单价',
      value: 'price',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    invalidQty: {
      label: '撤回数量',
      value: 'invalidQty',
      type: 'column',
      componentType: 'number',
      formatter: utils.qtyFormatter,
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
function _toSourceDetail(consumeOrderNo, sourceId) {
  shows.value = false
  toSourceDetail(proxy, consumeOrderNo, sourceId, { returnPath: '/stock/consignConsumeConfirmOrder' })
}
function showDetail(row) {
  detailId.value = row.consumeDetailId
  handleClose()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  const search = filterInfo.value.keyword
  if (search) {
    tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === 'qrCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'uniqueCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factorySubCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factoryMainCode') {
          return String(data[key]).indexOf(search) > -1
        }
      })
    })
  } else {
    tableInfo.value.data = tableInfo.value.copyData
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset', detailId.value)
}
function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey:
      'consignConsumeConfirmOrderDetailModifySingle-com.slip.spd.entity.vo.stock.ConsignConsumeConfirmOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      _tableInfo.api = props.api
      getList()
    }
  })
}
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
